const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    shi: ['请选择地区'],
    xianqu: ['请选择报考县区'],
    suoshudanwei: ['请选择所属单位'],
    gangweibianma: ['请选择岗位编码'],
    name: '',
    fenshu: '',
    phone: 0,
    theList: ''
  },

  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  paimingInput: function (e) {
    this.setData({
      paiming: e.detail.value
    })
    console.log(this.data.paiming)
  },
  fenshuInput: function (e) {
    this.setData({
      fenshu: e.detail.value
    })
    console.log(this.data.fenshu)
  },

  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('shi发送选择改变，携带值为', _.data.shi[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=50963&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'shi',
        grtext: _.data.shi[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择报考县区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].xianqu)
        }
        _.setData({
          xianqu: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let _ = this;
    console.log('xianqu发送选择改变，携带值为', _.data.xianqu[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=50963&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'xianqu',
        grtext: _.data.xianqu[num2],
        onefiled: 'shi',
        onetext: _.data.shi[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择所属单位')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].suoshudanwei)

        }
        _.setData({
          suoshudanwei: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let _ = this;
    console.log('suoshudanwei发送选择改变，携带值为', _.data.suoshudanwei[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=50963&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'suoshudanwei',
        grtext: _.data.suoshudanwei[num3],
        onefiled: 'shi',
        onetext: _.data.shi[this.data.index],
        twofiled: 'xianqu',
        twotext: _.data.xianqu[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择岗位编码')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gangweibianma)
        }
        _.setData({
          gangweibianma: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let _ = this;
    console.log('gangweibianma发送选择改变，携带值为', _.data.gangweibianma[num4]);
    this.setData({
      index4: e.detail.value
    })
  },


  onLoad: function (options) {
    var _ = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=50963&callback=?', //接收url
      data: {
        level: '1',
        shi: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择报考地市')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].shi)
        }
        _.setData({
          shi: list
        });
      }
    })
    var _ = this;
    console.log(options);
    _.setData({
      area: options.area,
      agent: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
  },

  b1: function () {
    wx.navigateTo({
      url: '../list/list',
    })
  },
  getPhoneNumber(e) {
    var _ = this;
    var area = _.data.area;
    var agent = _.data.agent;
    var page = _.data.page;
    var shi = _.data.shi[_.data.index];
    var xianqu = _.data.xianqu[_.data.index2];
    var suoshudanwei = _.data.suoshudanwei[_.data.index3];
    var gangweibianma = _.data.gangweibianma[_.data.index4];
    var fenshu = _.data.fenshu;
    var name = _.data.name;
    var paiming = _.data.paiming;
    if (!name || !fenshu || !paiming) {
      wx.showToast({
        title: '请完整填写信息！',
        icon: 'none'
      })
      return false
    }
    const reg = /[\u4e00-\u9fa5]/ig
    if (!reg.test(name)) {
      name = name.replace(reg, '')
      wx.showToast({
        title: '姓名只能填写汉字',
        icon: "none",
      })
      return false
    }

    const {
      code
    } = e.detail

    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击了拒绝
      _.hideModal();
    } else { //允许授权执行跳转
      wx.request({
        url: "https://zg99.offcn.com/index/wechat/getwxphone?actid=50963&callback=?", //手机号解密
        data: {
          code
        },
        success(res) {
          console.log('res', res.data);
          if (res.data.status == 1) {
            var phone = res.data.phone; //获取得到手机号
            console.log("手机号=", phone)
            wx.setStorageSync('sfphone', phone)
            _.setData({
              mobile: phone,
            })
            var fenshuSt = {
              shi,
              xianqu,
              suoshudanwei,
              gangweibianma,
              fenshu,
              phone
            }
            wx.setStorageSync('fenshuSt', fenshuSt)
            wx.request({ //发送请求
              url: 'https://zg99.offcn.com/index/shaifen/register?actid=50963&callback=?', //接收url
              data: {
                shi: shi,
                xianqu: xianqu,
                suoshudanwei: suoshudanwei,
                gangweibianma: gangweibianma,
                fenshu: fenshu,
                name: name,
                paiming: paiming,
                phone: phone,
                area: area,
                agent: agent,
                isagree: true
              }, //接收参数
              success(res) {
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                  jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                  var theJson = JSON.parse(jsonStr);
                }
                console.log(theJson)
                wx.navigateTo({
                  url: '../list/list',
                })
                // wx.request({ //发送请求
                //   url: 'https://zg99.offcn.com/index/shaifen/getphoneinfo?actid=50963&callback=?', //接收url
                //   data: {
                //     phone: phone,
                //     sstimes:Date.parse(new Date()),
                //     page: 1,
                //     limit: 20
                //   }, //接收参数
                //   success(res) {
                //     var jsonStr = res.data;
                //     jsonStr = jsonStr.replace("(", "");
                //     jsonStr = jsonStr.replace(")", "");
                //     if (typeof jsonStr != 'object') {
                //       jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                //       var theJson = JSON.parse(jsonStr);
                //     }
                //     console.log(theJson)

                //     _.setData({
                //       paihang: theJson.lists[0].paihang,
                //       fenshu: theJson.lists[0].fenshu,
                //       fixed: 1
                //     })
                //   }
                // })

              }
            })
            wx.request({
              url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
              method: 'POST',
              data: {
                activeId: 32,
                mobile: phone,
                areaCode: area,
                agent: agent,
                pageId: page
              },
              success: res => {

              }
            })

          } else {
            console.log("获取手机号失败 ", res.data.msg)
          }
        }
      })
    }




  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '三支一扶晒分查分差',
      path: 'pages/shaifen/index/index?area=' + this.data.area + '&agent=' + this.data.agent + '&page=' + this.data.page
    }
  }
})